<?php
namespace app\examScore\controller;
use app\common\Base;
use app\common\Excel;
use think\Db;
//--班级历年成绩分析
class ExamChartClazz extends Base {
	
	public function index() {
		return $this->fetch();
	}

	public function scoreChart() {
		$grade = input("grade");
		$subject = input("subject");
		$this->assign("grade", $grade);
		$this->assign("subject", $subject);
		$this->assign("clazz", $this::getCharParam());
		return $this->fetch();
	}

	public function pageQuery() {
		$grade = input("grade");
		$subject = input("subject");
		if(!$grade) return [];
		$result = Db::table("综合实力")->alias("z")
				  ->join("考试名称 a","a.ID = z.考试ID","INNER")
				  ->join("学期名称 b","a.学期ID=b.ID","INNER")
				  ->join("年级名称 c","a.年级ID=c.ID","INNER")
				  ->field("考试ID,年级名称+考试名称 考试名称,班级,".$subject."名次")
				  ->where("班级ID like '".$grade."%'")
				  ->order("考试ID,班级")
				  ->select();
		$result = selectResult($result);
		$resultList = [];
		$list = [];
		$idx = 0;
		foreach ($result as $key => $obj) {
			if(array_key_exists("考试名称", $list) && $list["考试名称"] == $obj["考试名称"]){
				$list[(intval($obj["班级"])-100)] = $obj[$subject."名次"];
				if(($key+1) == count($obj)) {
					$resultList[$idx] = $result;
				}
			}else {
				if(array_key_exists("考试名称", $list)) {
					$resultList[$idx] =  $list;
					$idx += 1;
					$list = [];
				}
				$list["考试名称"] = $obj["考试名称"];
				$list[(intval($obj["班级"])-100)] = $obj[$subject."名次"];
			}
		}
		return $resultList;
	}

	public function exportExcel() {
		$grade = input("grade");
		$subject = input("subject");
		if(!$grade) return [];
		$result = Db::table("综合实力")->alias("z")
				  ->join("考试名称 a","a.ID = z.考试ID","INNER")
				  ->join("学期名称 b","a.学期ID=b.ID","INNER")
				  ->join("年级名称 c","a.年级ID=c.ID","INNER")
				  ->field("z.ID,考试ID,年级名称+考试名称 考试名称,班级,".$subject."名次")
				  ->where("班级ID like '".$grade."%'")
				  ->order("考试ID,班级")
				  ->select();
		$result = selectResult($result);
		$resultList = [];
		$list = [];
		$idx = 0;
		foreach ($result as $key => $obj) {
			if(array_key_exists("考试名称", $list) && $list["考试名称"] == $obj["考试名称"]){
				$list[(intval($obj["班级"])-100)] = $obj[$subject."名次"];
				if(($key+1) == count($obj)) {
					$resultList[$idx] = $result;
				}
			}else {
				if(array_key_exists("考试名称", $list)) {
					$resultList[$idx] =  $list;
					$idx += 1;
					$list = [];
				}
				$list["考试名称"] = $obj["考试名称"];
				$list[(intval($obj["班级"])-100)] = $obj[$subject."名次"];
			}
		}
		$result = Db::table("综合实力")
		 		  ->field("right(MAX(班级),2) clazz")
		 		  ->where("班级ID like '".$grade."%'")
		 		  ->find();
		$idx = intval($result["clazz"]);
		//excel表头内容
		$header = [];
		$header["考试名称"] = "考试名称";
		for ($x=1; $x<=$idx; $x++) {
		  $header[$x."班"] = $x;
		} 	
		Excel::toExcel($resultList,'班级历年成绩-'.$grade,$header);
		exit;
	}

	function gradeList() {
		$result = Db::table("成绩汇总")
			   ->field("left(班级ID,4) value")
			   ->distinct(true)
			   ->order("value desc")
			   ->select();
		$list[0]=array("text"=>"请选择", "value"=>"");
		foreach($result as $key => $obj){
			$list[intval($key) + 1] = array("text"=>$obj["value"], "value"=>$obj["value"]);
	    }
	    return $list;
	}

	function getCharParam() {
		$grade = input("grade");
		$subject = input("subject");
		$result = Db::table("综合实力")
		 		  ->field("right(MAX(班级),2) clazz")
		 		  ->where("班级ID like '".$grade."%'")
		 		  ->find();
		return $result["clazz"];
	}
}
